package com.zr.repertory.warearea.service.impl;

import com.zr.config.ErrorMessage;
import com.zr.enumvo.WarehouseMarkEnum;
import com.zr.enumvo.WarehouseStatusEnum;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import com.zr.warehouse.mapper.WareAreaMapper;
import com.zr.warehouse.pojo.*;
import com.zr.warehouse.service.WareAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by HXH on 2019/11/21 0021.
 */
@Service
public class WareAreaServiceImpl implements WareAreaService{

    @Autowired
    private WareAreaMapper wareAreaMapper;


    //分页查询
    @Override
    public ResultVo queryAreaPage(WareAreaSelectVo wareAreaSelectVo) {
        //根据条件查询数量
        int count =  wareAreaMapper.queryCount(wareAreaSelectVo);
        //根据条件查询数据
        List<WareAreaVo> wareAreaVoList = wareAreaMapper.queryData(wareAreaSelectVo);
        //把0，1转换成汉字
        for (WareAreaVo wareAreaVo:wareAreaVoList){
            wareAreaVo.setWareAreaStatusName(WarehouseStatusEnum.getName(wareAreaVo.getWareAreaStatus()));
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(wareAreaSelectVo.getPageIndex());
        allRecords.setPageSize(wareAreaSelectVo.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setTotalNumber(count);
        allRecords.setDataList(wareAreaVoList);
        return ResultBuilderVo.success(allRecords);
    }

    //添加库区
    @Override
    @Transactional
    public ResultVo addWareArea(@Valid WareAreaAddVo wareAreaAddVo) {
//验证前端数据
            /*
            1.验证库房标识是否存在
            2.验证库房状态是否存在
            */
        ResultVo resultVo1 = validate(wareAreaAddVo);
        if (!resultVo1.getSuccess()){
            return  resultVo1;
        }
        //入库
        WareAreaVo wareAreaVo=new WareAreaVo();
        wareAreaMapper.addWareAreaVo(wareAreaVo);
        return ResultBuilderVo.success();
    }

    public ResultVo validate(@Valid WareAreaAddVo wareAreaAddVo) {
            //1.验证库区状态是否存在
        if (StringUtils.isEmpty(WarehouseStatusEnum.getName(wareAreaAddVo.getWareAreaStatus()))) {
            return ResultBuilderVo.error("库区" +
                    "状态不存在！");
        }
        //验证库区编码不能重复
        int countCode =  wareAreaMapper.queryByWareAreaCode(wareAreaAddVo.getWareAreaCode());
        if (countCode>0){
            return ResultBuilderVo.error(ErrorMessage.WAREAREACODEEXITS);
        }
        //验证库区名称不能重复
        int countName =  wareAreaMapper.queryByWarehouseName(wareAreaAddVo.getWareAreaName());
        if (countName>0){
            return ResultBuilderVo.error(ErrorMessage.WAREAREANAMEEXITS);
        }
        return ResultBuilderVo.success();
    }

}
